package com.conduit.comnands;

import java.awt.List;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Map;
import java.util.concurrent.Executors;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.conduit.autoit.AutoItAgent;
import com.conduit.commands.prototype.AbstractCommand;
import com.conduit.enums.AutoItCommands;
import com.conduit.enums.ResultCode;
import com.conduit.utils.Command;
import com.conduit.utils.Execute;

public class ExecuteFileCommand extends AbstractCommand {

	public ExecuteFileCommand() {
		commandSyntax = "execute file\\s{2,}(.*)";
	}

	@Override
	public void executeWithArgs(String args) {
		// validate command's syntax
		if (!args.matches(commandSyntax)) {
			setResult("Error in Command's syntax (\"" + args + "\")");
			setResultCode(ResultCode.Error);
			return;
		}
		Matcher matcher = Pattern.compile(commandSyntax).matcher(args);
		if (matcher.find()) {
			Command cmd = new Command();
			cmd.setCmd(matcher.group(1).split(" "));
			try{
			AutoItAgent.runCommand(AutoItCommands.RunWait, matcher.group(1));
			//Execute.execute(cmd,true,false,true);
			setResult("File " + matcher.group(1) + " Was Executed");
			setResultCode(ResultCode.Successes);
			}catch (Exception e ){
				setResult("File " + matcher.group(1) + " Was not Found");
				setResultCode(ResultCode.Error);
			}	
		}
	}

	@Override
	public String getCommandName() {
		return "Execute File";
	}

}
